<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>设置 DateTime 对象的时区</title>
<link media="all" rel="stylesheet" type="text/css" href="styles/03e73060321a0a848018724a6c83de7f-theme-base.css" />
<link media="all" rel="stylesheet" type="text/css" href="styles/03e73060321a0a848018724a6c83de7f-theme-medium.css" />

 </head>
 <body class="docs"><div class="navbar navbar-fixed-top">
  <div class="navbar-inner clearfix">
    <ul class="nav" style="width: 100%">
      <li style="float: left;"><a href="datetime.settimestamp.html">« DateTime::setTimestamp</a></li>
      <li style="float: right;"><a href="datetime.sub.html">DateTime::sub »</a></li>
    </ul>
  </div>
</div>
<div id="breadcrumbs" class="clearfix">
  <ul class="breadcrumbs-container">
    <li><a href="index.html">PHP Manual</a></li>
    <li><a href="class.datetime.html">DateTime</a></li>
    <li>设置 DateTime 对象的时区</li>
  </ul>
</div>
<div id="layout">
  <div id="layout-content"><div id="datetime.settimezone" class="refentry">
 <div class="refnamediv">
  <h1 class="refname">DateTime::setTimezone</h1>
  <h1 class="refname">date_timezone_set</h1>
  <p class="verinfo">(PHP 5 &gt;= 5.2.0, PHP 7, PHP 8)</p><p class="refpurpose"><span class="refname">DateTime::setTimezone</span> -- <span class="refname">date_timezone_set</span> &mdash; <span class="dc-title">设置 DateTime 对象的时区</span></p>

 </div>

 <div class="refsect1 description" id="refsect1-datetime.settimezone-description">
  <h3 class="title">说明</h3>
  <p class="para">面向对象风格</p>
  <div class="methodsynopsis dc-description">
   <span class="modifier">public</span> <span class="methodname"><strong>DateTime::setTimezone</strong></span>(<span class="methodparam"><span class="type"><a href="class.datetimezone.html" class="type DateTimeZone">DateTimeZone</a></span> <code class="parameter">$timezone</code></span>): <span class="type"><a href="class.datetime.html" class="type DateTime">DateTime</a></span></div>

  <p class="para rdfs-comment">过程化风格</p>
  <div class="methodsynopsis dc-description"><span class="methodname"><a href="function.date-timezone-set.html" class="methodname">date_timezone_set</a></span>(<span class="methodparam"><span class="type"><a href="class.datetime.html" class="type DateTime">DateTime</a></span> <code class="parameter">$object</code></span>, <span class="methodparam"><span class="type"><a href="class.datetimezone.html" class="type DateTimeZone">DateTimeZone</a></span> <code class="parameter">$timezone</code></span>): <span class="type"><a href="class.datetime.html" class="type DateTime">DateTime</a></span></div>

  <p class="para rdfs-comment">
   为 <span class="classname"><a href="class.datetime.html" class="classname">DateTime</a></span> <span class="type">object</span> 设置新时区。
  </p>
  <p class="para">
   跟 <span class="methodname"><a href="datetimeimmutable.settimezone.html" class="methodname">DateTimeImmutable::setTimezone()</a></span> 一样，但适用于
   <span class="classname"><a href="class.datetime.html" class="classname">DateTime</a></span>。
  </p>
  <p class="para">
   过程化版本将 <span class="classname"><a href="class.datetime.html" class="classname">DateTime</a></span> 对象作为第一个参数。
  </p>
 </div>


 <div class="refsect1 parameters" id="refsect1-datetime.settimezone-parameters">
  <h3 class="title">参数</h3>
  <dl>
   <dt>
<code class="parameter">object</code></dt>

<dd>
<p class="para">仅过程化风格：由 <span class="function"><a href="function.date-create.html" class="function">date_create()</a></span> 返回的 <span class="classname"><a href="class.datetime.html" class="classname">DateTime</a></span> 类型的对象。此函数会修改这个对象。</p></dd>

   
    <dt>
<code class="parameter">timezone</code></dt>

    <dd>

     <p class="para">
      代表所需时区的 <span class="classname"><a href="class.datetimezone.html" class="classname">DateTimeZone</a></span> 对象。
     </p>
    </dd>

   
  </dl>

 </div>


 <div class="refsect1 returnvalues" id="refsect1-datetime.settimezone-returnvalues">
  <h3 class="title">返回值</h3>
  <p class="para">
   返回链式调用的 <span class="classname"><a href="class.datetime.html" class="classname">DateTime</a></span> 对象。调用此方法时，底层的时间点不会更改。
  </p>
 </div>


 <div class="refsect1 examples" id="refsect1-datetime.settimezone-examples">
  <h3 class="title">范例</h3>
  <div class="example" id="example-2000">
   <p><strong>示例 #1 <span class="function"><strong>DateTime::setTimeZone()</strong></span> 例程</strong></p>
   <div class="example-contents"><p>面向对象风格</p></div>
   <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br />$date&nbsp;</span><span style="color: #007700">=&nbsp;new&nbsp;</span><span style="color: #0000BB">DateTime</span><span style="color: #007700">(</span><span style="color: #DD0000">'2000-01-01'</span><span style="color: #007700">,&nbsp;new&nbsp;</span><span style="color: #0000BB">DateTimeZone</span><span style="color: #007700">(</span><span style="color: #DD0000">'Pacific/Nauru'</span><span style="color: #007700">));<br />echo&nbsp;</span><span style="color: #0000BB">$date</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">format</span><span style="color: #007700">(</span><span style="color: #DD0000">'Y-m-d&nbsp;H:i:sP'</span><span style="color: #007700">)&nbsp;.&nbsp;</span><span style="color: #DD0000">"\n"</span><span style="color: #007700">;<br /><br /></span><span style="color: #0000BB">$date</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">setTimezone</span><span style="color: #007700">(new&nbsp;</span><span style="color: #0000BB">DateTimeZone</span><span style="color: #007700">(</span><span style="color: #DD0000">'Pacific/Chatham'</span><span style="color: #007700">));<br />echo&nbsp;</span><span style="color: #0000BB">$date</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">format</span><span style="color: #007700">(</span><span style="color: #DD0000">'Y-m-d&nbsp;H:i:sP'</span><span style="color: #007700">)&nbsp;.&nbsp;</span><span style="color: #DD0000">"\n"</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
   </div>

   <div class="example-contents"><p>过程化风格</p></div>
   <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br />$date&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">date_create</span><span style="color: #007700">(</span><span style="color: #DD0000">'2000-01-01'</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">timezone_open</span><span style="color: #007700">(</span><span style="color: #DD0000">'Pacific/Nauru'</span><span style="color: #007700">));<br />echo&nbsp;</span><span style="color: #0000BB">date_format</span><span style="color: #007700">(</span><span style="color: #0000BB">$date</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'Y-m-d&nbsp;H:i:sP'</span><span style="color: #007700">)&nbsp;.&nbsp;</span><span style="color: #DD0000">"\n"</span><span style="color: #007700">;<br /><br /></span><span style="color: #0000BB">date_timezone_set</span><span style="color: #007700">(</span><span style="color: #0000BB">$date</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">timezone_open</span><span style="color: #007700">(</span><span style="color: #DD0000">'Pacific/Chatham'</span><span style="color: #007700">));<br />echo&nbsp;</span><span style="color: #0000BB">date_format</span><span style="color: #007700">(</span><span style="color: #0000BB">$date</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'Y-m-d&nbsp;H:i:sP'</span><span style="color: #007700">)&nbsp;.&nbsp;</span><span style="color: #DD0000">"\n"</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
   </div>

   <div class="example-contents"><p>以上例程会输出：</p></div>
   <div class="example-contents screen">
<div class="cdata"><pre>
2000-01-01 00:00:00+12:00
2000-01-01 01:45:00+13:45
</pre></div>
   </div>
  </div>
 </div>


 <div class="refsect1 seealso" id="refsect1-datetime.settimezone-seealso">
  <h3 class="title">参见</h3>
  <ul class="simplelist">
   <li class="member"><span class="function"><a href="datetimeimmutable.settimezone.html" class="function" rel="rdfs-seeAlso">DateTimeImmutable::setTimezone()</a> - Sets the time zone</span></li>
   <li class="member"><span class="function"><a href="datetime.gettimezone.html" class="function" rel="rdfs-seeAlso">DateTime::getTimezone()</a> - Return time zone relative to given DateTime</span></li>
   <li class="member"><span class="function"><a href="datetimezone.construct.html" class="function" rel="rdfs-seeAlso">DateTimeZone::__construct()</a> - 创建新的DateTimeZone对象</span></li>
  </ul>
 </div>


</div></div></div></body></html>